package com.baaio.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class BaaioReflect
{

	public HashMap<String, String> getValue(Object object)
	{

		ArrayList<String> list = new ArrayList<String>();
		ArrayList<Object> list2 = new ArrayList<Object>();
		HashMap<String, String> map = new HashMap<String, String>();
		ArrayList<String> list3 = new ArrayList<String>();
		Class<?> classType = object.getClass();
		Field[] files = classType.getDeclaredFields();
		for (Field f : files)
		{
			String fl = f.getName();
			list.add("get" + fl.substring(0, 1).toUpperCase() + fl.substring(1));
			list3.add(fl);
		}
		int k = 0;
		for (String str : list)
		{

			Method obj;
			try
			{
				obj = classType.getMethod(str, new Class[] {});
				Object obj2 = obj.invoke(object, new Object[] {});
				map.put(list3.get(k++), obj2.toString());
				list2.add(obj2);
			}
			catch (Exception e)
			{
				k++;
			}

		}

		return map;
	}

	public HashMap<String, String> getKeyValue(Object object)
	{

		Method obj;
		HashMap<String, String> map = new HashMap<String, String>();
		try
		{
			obj = getMethod(object, "getPRIMARYKEY");

			String obj2 = (String) obj.invoke(object, new Object[] {});
			map.put("baaio-key", obj2);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return map;
	}
	
	public HashMap<String, String> getKeyAllValue(Object object,ArrayList<String> list)
	{

		Method obj;
		HashMap<String, String> map = new HashMap<String, String>();
		
		Method[] md = object.getClass().getMethods();
		try {
			for (String str : list) {
				String mdName = "get" + str.substring(0, 1).toUpperCase()
						+ str.substring(1);
		
		for(int i = 0; i < md.length; i ++)
		{
			if(md[i].getName().equals(mdName))
			{
				try
				{
					obj = getMethod(object, md[i].getName());

					String obj2 = (String) obj.invoke(object, new Object[] {});
					if(obj2 != null){
					map.put(md[i].getName(), obj2);
					}
				}
				catch (Exception e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		return map;
	}

	public Method getMethod(Object object, String methodName)
	{

		Class<?> classType = object.getClass();
		Method obj = null;
		try
		{
			obj = classType.getMethod(methodName, new Class[] {});
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return obj;
	}

	public HashMap<String, String> getOneToMany(Object object)
	{

		HashMap<String, String> map = new HashMap<String, String>();
		Method method1;
		try
		{
			method1 = getMethod(object, "getMeToOne");
			Method m1 = getMethod(object, "setRelation");
			m1.invoke(object, new Object[] {});
			@SuppressWarnings("unchecked")
			HashMap<String, Object> obj2 = (HashMap<String, Object>) method1
					.invoke(object, new Object[] {});
			Set<String> set = obj2.keySet();

			for (Iterator<String> i = set.iterator(); i.hasNext();)
			{
				String key = i.next();
				Object obj3 = obj2.get(key);
				String tableName = getTableName(obj3);
				Method method2 = getMethod(obj3, "getOneToMe");
				Method m2 = getMethod(obj3, "setRelation");
				m2.invoke(obj3, new Object[] {});
				@SuppressWarnings("unchecked")
				HashMap<Object, String> obj4 = (HashMap<Object, String>) method2
						.invoke(obj3, new Object[] {});
				Set<Object> set2 = obj4.keySet();
				String vName;
				String relaValue = null;
				map.put("baaioKey", key);
				for (Iterator<Object> ii = set2.iterator(); ii.hasNext();)
				{
					Object ob = ii.next();
					String tName = getTableName(ob);
					vName = getTableName(object);

					if (tName.equals(vName))
					{
						relaValue = obj4.get(ob);
					}
				}

				map.put(tableName, relaValue);
			}

		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return map;
	}

	public ArrayList<String> getItems(Object object)
	{

		ArrayList<String> list3 = new ArrayList<String>();
		Class<?> classType = object.getClass();
		Field[] files = classType.getDeclaredFields();
		for (Field f : files)
		{
			list3.add(f.getName());
		}

		return list3;
	}

	public String getUpConditionString(Object object)
	{

		HashMap<String, String> map = getValue(object);
		StringBuffer buffer = new StringBuffer();
		buffer.append("where ");

		Set<String> set = map.keySet();
		for (Iterator<String> i = set.iterator(); i.hasNext();)
		{
			String key = i.next();

			buffer.append(key + "='" + map.get(key) + "'");
			if (i.hasNext())
			{
				buffer.append(" and ");
			}
		}

		return buffer.toString();
	}

	public String getTableName(Object object)
	{

		return object.getClass().getSimpleName().toLowerCase();
	}

}
